extends CharacterBody2D

@onready var bubble_dialog = preload("res://scenes/bubble_dialog.tscn")
var current_dialog: Control = null

@export var max_health: float = 100
@export var move_speed: float = 100
@export var attack_damage: float = 10

var current_health: float
var dialog_data = [
	{ "name": "村长", "text": "你好,旅行者" },
	{ "name": "玩家", "text": "你好,请问这里是什么地方?" },
	{ "name": "村长", "text": "这里是和平村,一个远离纷争的小村庄。" }
]

func _ready():
	current_health = max_health
	
	# 创建对话框
	current_dialog = bubble_dialog.instantiate()
	add_child(current_dialog)
	current_dialog.hide()
	
	# 创建交互区域
	var interaction_area = Area2D.new()
	interaction_area.name = "InteractionArea"
	add_child(interaction_area)
	
	# 创建碰撞形状
	var collision_shape = CollisionShape2D.new()
	var circle_shape = CircleShape2D.new()
	circle_shape.radius = 50.0
	collision_shape.shape = circle_shape
	interaction_area.add_child(collision_shape)
	
	# 连接信号
	interaction_area.body_entered.connect(_on_interaction_area_body_entered)

func interact():
	if current_dialog and not current_dialog.visible:
		var dialog_text = "你好，旅行者！要不要学习剑术？"
		current_dialog.show_dialog(dialog_text)
		current_dialog.update_position(global_position)

func _process(_delta):
	if current_dialog and current_dialog.visible:
		current_dialog.update_position(global_position)

func _on_interaction_area_body_entered(body: Node2D) -> void:
	if body.is_in_group("player"):
		interact() 
